<?php

namespace app\admin\controller;

use app\admin\model\member\Level;
use app\admin\model\Role;
use app\admin\model\shareholder\ShareholderModel;
use app\admin\model\Store as modelStore;
use app\BaseController;
use app\Request;
use Tool\QrCodeImage;

class Shareholder extends BaseController
{


    public function shareholderList(){


        if (\request()->isAjax() && \request()->isGet()) {

            $result = ShareholderModel::getList();;

            return $this->success($result);
        }


        return view();
    }



    public function editShareholder(Request $request)
    {


        $id = input('id');
        if ($request->isPost() && $request->isAjax()) {

            $id = input('id');

            empty($id) ? $type = 'add' : $type = 'edit';
            if ($type == 'add') {
                $result = ShareholderModel::insertShareholder(input(''));
            } else {

                $result = ShareholderModel::modifyShareholder(input(''));
            }
            return json($result);
        }

        $info = ShareholderModel::where(['id' => $id])->find();
        $info ? $info = $info->getData() : $info = [];

        empty($id) ? $tpl = 'add_shareholder' : $tpl = 'edit_shareholder';

        $shareholder_level  = config('view.shareholder_level');


        return \view($tpl, ['info' => $info,'shareholder_level'=>$shareholder_level]);
    }

    public function shareholderDel(): array
    {

        $id=input('id');
        $res=ShareholderModel::where(['id'=>$id])->delete();
        if(!empty($res)){
            $msg=[
                'code'=>200,
                'msg'=>'success'
            ];
        }else{
            $msg=[
                'code'=>40040,
                'msg'=>'删除失败'
            ];
        }

        return $msg;

    }

    public function shareholderQrcode(): string
    {

        $id=input('id');
        $name=ShareholderModel::where(['id'=>$id])->value('name');

        return QrCodeImage::mkCode('https://www.baidu.com',false,null,$name);
    }


    /*****
     * @description  联创等级管理
     * @return \think\response\Json|\think\response\View
     */
    public function shareholderLevel()
    {


        if (\request()->isAjax() && \request()->isGet()) {

            $result = Level::getList();;

            return $this->success($result);
        }



        return view();
    }


    public function editShareholderLevel(Request $request)
    {


        $id = input('id');
        if ($request->isPost() && $request->isAjax()) {

            $id = input('id');

            empty($id) ? $type = 'add' : $type = 'edit';
            if ($type == 'add') {
                $result = Level::insertLevel(input(''));
            } else {

                $result = Level::modifyLevel(input(''));
            }
            return json($result);
        }

        $info = Level::where(['id' => $id])->find();
        $info ? $info = $info->getData() : $info = [];

        empty($id) ? $tpl = 'add_shareholder_level' : $tpl = 'edit_shareholder_level';

        $shareholder_level  = config('view.shareholder_level');


        return \view($tpl, ['info' => $info,'shareholder_level'=>$shareholder_level]);
    }

}